---
title: "study2-data-tables"
output: html_document
---

<!-- Set working directory -->

```{r setup, include=FALSE, cache=FALSE}
rm(list=ls())
require("knitr")



# set your own working directory by adjusting the below line - to where you saved the replication files:
#eg knitr::opts_knit$set(root.dir = "~/Downloads/replication-files")

knitr::opts_knit$set(root.dir = "C:/Users/qs932404/Dropbox/USmidterms/R&R EJPR/Final submission/replication-files")
```


<!--Load all the required libraries in-->

```{r libraries, include=FALSE, cache = FALSE}

library(tidyr)

library(dplyr)

library(stringr)

```


```{r theme}


theme_report_forcovar_wlegend <- function(base_size = 14) {
  theme_bw(base_size = base_size) %+replace%
    theme(
      # L'ensemble de la figure
      plot.title = element_text(size = rel(1), face = "bold", margin = margin(0,0,5,0), hjust = 0),
      # Zone où se situe le graphique
      panel.grid.minor = element_blank(),
       panel.grid.major = element_blank(),
      panel.border = element_blank(),
      # Les axes
      axis.title = element_text(size = rel(0.85), face = "bold"),
      axis.text = element_text(size = rel(0.70), face = "bold"),
      axis.line = element_line(color = "black"),
      # La légende
      #legend.position = "none",
      legend.title = element_text(size = rel(0.85), face = "bold"),
      legend.text = element_text(size = rel(0.70), face = "bold"),
      legend.key = element_rect(fill = "transparent", colour = NA),
      legend.key.size = unit(1.5, "lines"),
      legend.background = element_rect(fill = "transparent", colour = NA),
      # Les étiquettes dans le cas d'un facetting
      strip.background = element_rect(fill = "#17252D", color = "#17252D"),
      strip.text = element_text(size = rel(0.85), face = "bold", color = "white", margin = margin(5,0,5,0))
    )
}




```


# Load data

```{r load}
data <- read.csv("study2_dataset.csv")

```


# Recode political violence outcome

```{r recodedv}



#recode into binary


data <- data %>% mutate(
  pv1_bin = case_when(str_detect(polviol_msgleaders, "Never") ~0, 
                TRUE ~ 1),
   pv2_bin = case_when(str_detect(polviol_harassordinary, "Never") ~0, 
                TRUE ~ 1),
   pv3_bin = case_when(str_detect(polviol_advance, "Not at all") ~0, 
                TRUE ~ 1),
   pv4_bin = case_when(str_detect(polviol_elec, "Not at all") ~0, 
                TRUE ~ 1)
)

#recode into numeric
data <- data %>%
  mutate(across(c(polviol_msgleaders,polviol_harassordinary), ~ factor(.x , levels= c("Never", "Occasionally","Frequently",  "Always"))))

data <- data %>%
  mutate(across(c(polviol_advance, polviol_elec), ~ factor(.x , levels= c("Not at all", "A little", "A moderate amount", "A lot", "A great deal"))))

#recode into normalized political violence variable and index
data <- data %>% rowwise() %>%
  mutate (pv1_normalized = (as.numeric(polviol_msgleaders) - 1) / (4-1),
          pv2_normalized = (as.numeric(polviol_harassordinary) - 1) / (4-1),
          pv3_normalized = (as.numeric(polviol_advance) - 1) / (5-1),
          pv4_normalized = (as.numeric(polviol_elec) - 1) / (5-1),
          viol_index = mean(c(pv1_normalized,pv2_normalized,pv3_normalized), na.rm=T))


```

# Recode democratic norms index

```{r demnorms}



# recode into factor
# higher values = pro democratic norms
data <- data %>%
  mutate(across(c(dn2:dn8), ~ factor(.x , levels= c("Strongly agree", "Agree", "Somewhat agree", "Neither agree nor disagree", "Somewhat disagree", "Disagree", "Strongly disagree"))))


data <- data %>%
  mutate(across(c(dn1), ~ factor(.x , levels= c("Strongly disagree", "Disagree", "Somewhat disagree", "Neither agree nor disagree", "Somewhat agree", "Agree", "Strongly agree"))))

# index


data <- data %>%
 mutate(across(dn1:dn8, as.numeric)) %>% # Convert dn1 to dn8 to numeric
mutate(demnorms_index = rowMeans(across(dn1:dn8), na.rm = TRUE))# Compute row-wise mean


```
# Recode affective polarization


```{r aprecode}
 

data <- data %>% mutate(therm_votersoutparty = as.numeric(Feeling_Thermometer_1),
                therm_votersinparty = as.numeric(Feeling_Thermometer_2),
                therm_polinparty = as.numeric(Feeling_Thermometer_8),
                therm_poloutparty = as.numeric(Feeling_Thermometer_9))
data <- data %>% mutate(ap_voters = therm_votersinparty - therm_votersoutparty,
                        ap_party =  therm_polinparty -  therm_poloutparty)


```


# Recode treatment variable

```{r recode}


data$treatment_factor <- as.factor(data$treatment)
data$treatment_factor <- relevel(data$treatment_factor, ref = "control")

data %>% count(treatment_factor)

data <- data  %>% mutate(treatment_pooled = case_when(treatment_factor%in% c("anger", "fear") ~ "Pooled",
                                                      treatment_factor=="control" ~ "control"))
```


# Recode demographic variables 

```{r recode_demvars, echo=F}



data <- data %>% mutate(w1_gender_female = case_when(dem_Sex=="Female" ~1,
                                                     TRUE~ 0),
                        w1_education_advanced = case_when(grepl( "degree", dem_Highest.education.level.completed) ~ 1,
                                                          TRUE ~ 0),
                        w1_ethnicity_white = case_when(grepl("White", dem_Ethnicity.simplified) ~1,
                                                       TRUE ~0),
                    w1_age = as.numeric(dem_Age),
                        w1_employ = case_when(dem_Employment.status== "Full-Time" ~1,
                                              TRUE ~0))




```

# Recode party importance (pre-treatment)

```{r recodepimport}


# Recode pidstrength into numeric values from 1 to 5
data <- data %>%
  mutate(pidstrength_numeric = recode(pidstrength,
    "Not at all important" = 1,
    "A little important" = 2,
    "Moderately important" = 3,
    "Very important" = 4,
    "Extremely important" = 5,
    "Prefer not to say" = NA_real_
  ))




```

# Table: Overview of experimental conditions, column: "N Study 2"
```{r tabexp}
data %>% count(treatment_factor, treatment_pooled)
```
# Regression function

```{r regfunc}
# takes dependent variable list as input
#returns clean dataframe with regression output for pooled treatment and separate treatments


run_regression_models <- function(dv_cols, data) {
  library(dplyr)
  library(stringr)
  
  output <- list()
  output_df_list <- list()
  
  # Loop for treatment_factor
  for (i in seq_along(dv_cols)) {
    model <- lm_robust(as.formula(paste(dv_cols[[i]], "~ treatment_factor")), data = data)
    output[[paste0(dv_cols[[i]], "_factor")]] <- model
    
    output_df_list[[length(output_df_list) + 1]] <- data.frame(
      Model = paste0(dv_cols[[i]], " (factor)"),
      Covariate = names(model$coefficients),
      Beta_Estimate = model$coefficients,
      CI_95_LL = confint(model)[, 1],
      CI_95_UL = confint(model)[, 2],
      P_val = summary(model)$coefficients[, 4],
      stringsAsFactors = FALSE
    )
  }
  
  # Loop for treatment_pooled
  for (i in seq_along(dv_cols)) {
    model <- lm_robust(as.formula(paste(dv_cols[[i]], "~ treatment_pooled")), data = data)
    output[[paste0(dv_cols[[i]], "_pooled")]] <- model
    
    output_df_list[[length(output_df_list) + 1]] <- data.frame(
      Model = paste0(dv_cols[[i]], " (pooled)"),
      Covariate = names(model$coefficients),
      Beta_Estimate = model$coefficients,
      CI_95_LL = confint(model)[, 1],
      CI_95_UL = confint(model)[, 2],
      P_val = summary(model)$coefficients[, 4],
      stringsAsFactors = FALSE
    )
  }
  
  # Combine and annotate
  output_df_full <- do.call(rbind, output_df_list) %>%
    mutate(treatment = case_when(
      str_detect(tolower(Covariate), "anger") ~ "Anger",
      str_detect(tolower(Covariate), "fear") ~ "Worry",
      str_detect(Covariate, "Intercept") ~ "Placebo",
      str_detect(Covariate, "Pooled") ~ "Pooled",
      TRUE ~ NA_character_
    ))
  
  return(output_df_full)
}

```

# Effect on support for political violence
```{r fig1}
library(estimatr)

dv_cols <- data %>% select(starts_with("pv")) %>% select(ends_with("normalized")) %>%  colnames(.) %>% as.list() #%>% 

dv_cols <- append(dv_cols, "viol_index")


output_df_full <- run_regression_models(dv_cols, data)

output_df_full <- output_df_full %>%  mutate(dv = case_when(
  str_detect(Model, "pv1") ~ "PV 1",
  str_detect(Model, "pv2") ~ "PV 2",
  str_detect(Model, "pv3") ~ "PV 3",
  str_detect(Model, "pv4") ~ "PV 4",
  str_detect(Model, "index") ~ "PV index"),
  dv = factor(dv, levels = c("PV index", "PV 1", "PV 2", "PV 3","PV 4")))


saveRDS(output_df_full, "data_for_figures/s2_f8_main_polviol.rds")


```

# Demcoratic norms

```{r demnorms}



dv_cols <- data %>% select(starts_with("dn"))   %>% colnames(.) %>% as.list() #%>% 
dv_cols <- append(dv_cols, "demnorms_index")

output_df_full <- run_regression_models(dv_cols, data)

output_df_full <- output_df_full %>%  mutate(dv = case_when(
  str_detect(Model, "dn1") ~ "DN 1",
  str_detect(Model, "dn2") ~ "DN 2",
  str_detect(Model, "dn3") ~ "DN 3",
  str_detect(Model, "dn4") ~ "DN 4",
  str_detect(Model, "dn5") ~ "DN 5",
  str_detect(Model, "dn6") ~ "DN 6",
  str_detect(Model, "dn7") ~ "DN 7",
  str_detect(Model, "dn8") ~ "DN 8",
  str_detect(Model, "index") ~ "DN index"),
  dv = factor(dv, levels = c("DN index", "DN 1", "DN 2", "DN 3","DN 4", "DN 5", "DN 6", "DN 7", "DN 8")))


saveRDS(output_df_full, "data_for_figures/s2_f9_main_demnorms.rds")





```


## Affective polarization

```{r ap}
dv_cols <- data %>% select(starts_with(c("ap_", "therm")))   %>% colnames(.) %>% as.list() 


output_df_full <- run_regression_models(dv_cols, data)

saveRDS(output_df_full, "data_for_figures/s2_f7_main_ap.rds")



```

# Emotions



```{r manipemos}

data <- data %>%
 mutate(across(enthusiastic:resigned, as.numeric))
dv_cols <- data %>% select(enthusiastic:resigned) %>% colnames(.) %>% as.list() 


output_df_full <- run_regression_models(dv_cols, data)


output_df_full <- output_df_full %>% mutate ( dv =str_extract(Model, "^[^\\s]+"))


output_df_full <- output_df_full %>% mutate ( dv = factor(dv, levels = c("sad", "resigned","enthusiastic", "hopeful", "proud","indifferent","disgusted","disappointed", "anxious", "scared", "afraid", "bitter", "resentful", "hateful", "outraged", "worried", "angry")),
emotiongroup= case_when(str_detect(dv, "angry|worried")~ "main",
TRUE ~ "secondary"))




saveRDS(output_df_full, "data_for_figures/s2_f6_main_emo.rds")


```

# Binarised emotion graphs: worried and angry

# Graph for binary emotion outcomes: worried and angry 

```{r emobin}


library(broom)

data <- data %>% mutate(angry_bin = case_when(angry<4 ~ 0,
                                     angry>=4 & angry<6 ~ 1))



summaryres <-
 data %>% 
  group_by(treatment_factor) %>%
  do(tidy(lm_robust(angry_bin ~ 1, data = .))) %>%
  mutate(predicted = estimate) %>%
  mutate(conf.low.83 = estimate -(std.error*1.3722),
                          conf.high.83 = estimate + (std.error*1.3722),
         conf.low = estimate -(std.error*1.96),
                          conf.high = estimate + (std.error*1.96)) 

summaryres <- summaryres %>% mutate(Covariate = treatment_factor,
  treatment= case_when(str_detect(tolower(Covariate), "anger") ~ "Anger",
                                                                 str_detect(tolower(Covariate), "fear") ~ "Worry",
                                                               str_detect(Covariate, "control") ~ "Placebo"),
  treatment = factor(treatment, levels=c("Placebo", "Anger", "Worry")))
saveRDS(summaryres, "data_for_figures/s2_main_emobin_angry.rds")

# worried data

data <- data %>% mutate(worried_bin = case_when(worried<4 ~ 0,
                                      worried>=4 & worried<6 ~ 1))






summaryres <-
  data %>% 
  group_by(treatment_factor) %>%
  do(tidy(lm_robust(worried_bin ~ 1, data = .))) %>%
  mutate(predicted = estimate) %>%
  mutate(conf.low.83 = estimate -(std.error*1.3722),
                          conf.high.83 = estimate + (std.error*1.3722),
         conf.low = estimate -(std.error*1.96),
                          conf.high = estimate + (std.error*1.96)) 

summaryres <- summaryres %>% mutate(Covariate = treatment_factor,
  treatment= case_when(str_detect(tolower(Covariate), "anger") ~ "Anger",
                                                                 str_detect(tolower(Covariate), "fear") ~ "Worry",
                                                               str_detect(Covariate, "control") ~ "Placebo"),
  treatment = factor(treatment, levels=c("Placebo", "Anger", "Worry")))

saveRDS(summaryres, "data_for_figures/s2_main_emobin_worried.rds")
```
## Accuracy of election outcome

```{r accurate}

library(broom)
data <- data %>% mutate(treat_elecoutcome_correct = case_when(manip.won=="Donald Trump" ~ 1,
 TRUE ~0 ))


summaryres <-
  data %>% 
  group_by(treatment_factor) %>%
  do(tidy(lm_robust(treat_elecoutcome_correct ~ 1, data = .))) %>%
  mutate(predicted = estimate) %>%
  mutate(conf.low.83 = estimate -(std.error*1.3722),
                          conf.high.83 = estimate + (std.error*1.3722),
         conf.low = estimate -(std.error*1.96),
                          conf.high = estimate + (std.error*1.96)) 

summaryres <- summaryres %>% mutate(Covariate = treatment_factor,
  treatment= case_when(str_detect(tolower(Covariate), "anger") ~ "Anger",
                                                                 str_detect(tolower(Covariate), "fear") ~ "Worry",
                                                               str_detect(Covariate, "control") ~ "Placebo"),
  treatment = factor(treatment, levels=c("Placebo", "Anger", "Worry")))
  
lm_robust1 <- lm_robust(treat_elecoutcome_correct ~ treatment_factor, data=data)  



saveRDS(summaryres, "data_for_figures/s2_f5_main_accuracy.rds")



```


# Text analysis outcomes


# prepare corpus

```{r prepcorp}
library(tidytext)


data <- data %>% ungroup() %>% mutate(X= row_number())
data %>% count(X)


tidy_opentext <- data %>% 
    select(X, treatment_factor,pidstrength, emo_open) %>%
    unnest_tokens("word", emo_open)

tidy_countwords <- tidy_opentext %>% group_by(X) %>% count() 

data <- left_join(data, tidy_countwords, by="X") 


```

```{r sentimentbing, echo=F}


library(stringr)
head(get_sentiments("bing"),50)

dislike_sentiment <- tidy_opentext %>%
  inner_join(get_sentiments("bing")) %>%
      filter(sentiment=="negative") %>%
    count(X, sentiment) %>%
  mutate(sentiment_negative = n) %>%
  select(X, sentiment_negative)



data <- data %>% full_join(., dislike_sentiment, by= "X")

data <- data %>% mutate(sentiment_negative = replace_na(sentiment_negative, 0))




dislike_sentiment <- tidy_opentext %>%
  inner_join(get_sentiments("bing")) %>%
      filter(sentiment=="positive") %>%
    count(X, sentiment) %>%
  mutate(sentiment_positive = n) %>%
  select(X, sentiment_positive)



data <- data %>% full_join(., dislike_sentiment, by= "X")

data <- data %>% mutate(sentiment_positive = replace_na(sentiment_positive, 0))

```


```{r afinn}
#install.packages("textdata")
library(textdata)
dislike_sentiment <- tidy_opentext %>%
  inner_join(get_sentiments("afinn")) %>%
group_by(X) %>%
  mutate(sentiment_negative_afinn = (sum(value))*-1) %>% select(X, sentiment_negative_afinn) %>% unique() 
  



data <- data %>% full_join(., dislike_sentiment, by= "X")

data <- data %>% mutate(sentiment_negative_afinn = replace_na(sentiment_negative_afinn, 0))


```

```{r nrc}

# Define the URL and destination path

url <- "https://saifmohammad.com/WebDocs/Lexicons/NRC-Emotion-Lexicon.zip"
dest_zip <- "nrc_emotion_lexicon.zip"
dest_dir <- "nrc_lexicon"

# Create directory if it doesn't exist
if (!dir.exists(dest_dir)) {
  dir.create(dest_dir)
}

# Download the zip file
download.file(url, destfile = file.path(dest_dir, dest_zip), mode = "wb")

# Unzip the file
unzip(file.path(dest_dir, dest_zip), exdir = dest_dir)

# Define the path to the specific file inside the unzipped folder
lexicon_file <- file.path(dest_dir, "NRC-Emotion-Lexicon", 
                          "NRC-Emotion-Lexicon-Wordlevel-v0.92.txt")

# Read the file
nrc <- read.delim(lexicon_file,
                  header = FALSE,
                  stringsAsFactors = FALSE,
                  sep = "\t")

# Rename columns for clarity
colnames(nrc) <- c("word", "emotion", "association")

# Preview the data
head(nrc)

nrc_fear <- nrc %>%
      filter(emotion=="fear") %>%
  filter(association==1)


nrc_anger <- nrc %>%
      filter(emotion=="anger")%>%
  filter(association==1)

nrc_fear_unique <- nrc_fear %>%
  anti_join(., nrc_anger, by="word")

nrc_anger_unique <- nrc_anger %>%
  anti_join(., nrc_fear, by="word")

# match on all anger and fear words even though there is overlap

dislike_sentiment <- tidy_opentext %>%
  inner_join(nrc_anger) %>%
    count(X, emotion) %>%
  mutate(sentiment_negative_anger = n) %>%
  select(X, sentiment_negative_anger)



data <- data %>% full_join(., dislike_sentiment, by= "X")

data <- data %>% mutate(sentiment_negative_anger = replace_na(sentiment_negative_anger, 0))

dislike_sentiment <- tidy_opentext %>%
  inner_join(nrc_fear) %>%
    count(X, emotion) %>%
  mutate(sentiment_negative_fear = n) %>%
  select(X, sentiment_negative_fear)



data <- data %>% full_join(., dislike_sentiment, by= "X")

data <- data %>% mutate(sentiment_negative_fear = replace_na(sentiment_negative_fear, 0))



```

```{r textgraphs}

## combined graph for all sentiment measures



dv_cols <- data %>% select(starts_with("sentiment")) %>% colnames(.) %>% as.list()


output_df_full <- run_regression_models(dv_cols, data)

output_df_full <- output_df_full %>% mutate(emotion = Model,
sentiment = case_when(str_detect(emotion,"sentiment_negative ")~ "negative sentiment (Bing)",
                      str_detect(emotion,"sentiment_positive")~ "positive sentiment (Bing)",
                    str_detect(emotion,"negative_anger") ~"anger (NRC)",
                    str_detect(emotion,"negative_fear") ~ "fear (NRC)",
                   str_detect(emotion,"negative_afinn") ~ "negative sentiment (Afinn)"))


# pull standard deviation out from data

output_df_full <- output_df_full %>% mutate(sdest = case_when(sentiment== "negative sentiment (Bing)" ~ sd(data$sentiment_negative),
sentiment=="positive sentiment (Bing)" ~ sd(data$sentiment_positive),
                                 str_detect(sentiment, "Afinn") ~ sd(data$sentiment_negative_afinn),
                                 str_detect(sentiment, "anger") ~ sd(data$sentiment_negative_anger),
                                  str_detect(sentiment, "fear") ~ sd(data$sentiment_negative_fear)))
# create standardised coefficients

output_df_full <- output_df_full  %>% mutate(across(Beta_Estimate:CI_95_UL, ~ .x/sdest))

saveRDS(output_df_full, "data_for_figures/s2_f4_main_opentext.rds")




```

# Emotion factors

```{r emofacs}


data <- data %>% rowwise() %>% mutate(w2_emo_aversionfactor_num = mean(c(outraged, angry, hateful, bitter, resentful), na.rm=T),
                w2_emo_anxietyfactor_num = mean(c(worried, anxious, afraid, scared), na.rm=T),
                w2_emo_enthusiasmfactor_num = mean(c(enthusiastic, hopeful, proud), na.rm=T),
                w2_emo_disgustfactor_num= mean(c(disgusted, disappointed, indifferent), na.rm=T))

emo_cols <- data %>% select(starts_with("w2_emo")) %>% select(contains("num")) %>% select(contains("factor")) %>% colnames(.) %>% as.list()


output_df_full <- run_regression_models(emo_cols, data)


output_df_full <- output_df_full %>% mutate ( dv =str_extract(Model, "(?<=w2_emo_)[^_]+"))

saveRDS(output_df_full, "data_for_figures/s2_f6b_main_emofactors.rds")



```
# SI figures - distribution of variables

```{r createdv-overview-graphs}


library(tidyverse)
#install.packages("ggridges")
library(ggridges)


emo_cols <- data %>% select(starts_with("w2_emo")) %>% select(contains("num")) %>% select(contains("factor")) %>% colnames(.)

data <- data %>% mutate(pid_cat = "Democrats")
  
dfs<- data %>% dplyr::select(all_of(c(emo_cols, "pid_cat"))) %>%
  mutate_all(~as.character(.)) %>%
pivot_longer(
cols = starts_with("w2_emo"),
names_to = "category",
values_to = "value") %>%
  mutate(party_category=case_when(str_detect(category, "inparty")~ "In-party",
                            str_detect(category, "outparty") ~ "Out-party"),
         emotion_category = case_when(str_detect(category, "anxiety")~ "Anxiety",
                            str_detect(category, "aversion") ~ "Aversion",
                            str_detect(category, "disgust")~ "Disgust",
                            str_detect(category, "enthusiasm") ~ "Enthusiasm")) %>%
  mutate(value=as.numeric(value))
  
  
  

library(ggplot2)
ggplot(dfs, aes(x = value, y = emotion_category, fill=emotion_category)) +
  geom_density_ridges(
    jittered_points = TRUE,
    point_shape = "|", point_size = 2, size = 0.25,
    position = position_points_jitter(height = 0), alpha = 0.6, scale = 1.2,
    quantile_lines=TRUE, quantiles=2) +
  theme_classic()+
       labs(x = "Value", y="",title = "Distribution of emotions towards election outcome (Study 2)")+
  theme(
plot.title =element_text(size=16, colour="black", face = "bold"),
plot.subtitle = element_text(size=15, colour="black"),
axis.title.y = element_text(size=16, colour="black"),
axis.title.x = element_text(size=16,colour="black"),
axis.text.x = element_text(size=16,colour="black"),
axis.text.y =element_text(size=16,colour="black"),
legend.text = element_text(size = 16,colour="black"),
strip.text.x = element_text(size = 16,colour="black"),
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_blank(),
legend.position = "none",
panel.grid.major = element_blank(), panel.grid.minor = element_blank()
) +
  facet_wrap(~pid_cat)

ggsave(file= paste("figures/si_s2_desc_emodimensions.png", sep=""),width =25, height = 17, units = "cm", dpi=320)



```

## Distribution graphs for Appendix: PV Index and DN index, AP


```{r createdv-overview-graphs-pvdn}



dfs<- data %>% dplyr::select(all_of(c("demnorms_index", "pid_cat")))%>%
  mutate_all(~as.character(.)) %>%
pivot_longer(
cols = c("demnorms_index"),
names_to = "category",
values_to = "value") %>%
  mutate(emotion_category = case_when(str_detect(category, "viol")~ "PV index",
                            str_detect(category, "demnorms") ~ "DN index")) %>%
  mutate(value=as.numeric(value))
  
  
  

library(ggplot2)
ggplot(dfs, aes(x = value, y = emotion_category, fill=emotion_category)) +
  geom_density_ridges(
    jittered_points = TRUE,
    point_shape = "|", point_size = 2, size = 0.25,
    position = position_points_jitter(height = 0), alpha = 0.6, scale = 1.2,
    quantile_lines=TRUE, quantiles=2) +
  theme_classic()+
       labs(x = "Value", y="",title = "Distribution of support for democratic norms (Study 2)")+
  theme(
plot.title =element_text(size=16, colour="black", face = "bold"),
plot.subtitle = element_text(size=15, colour="black"),
axis.title.y = element_text(size=16, colour="black"),
axis.title.x = element_text(size=16,colour="black"),
axis.text.x = element_text(size=16,colour="black"),
axis.text.y =element_text(size=16,colour="black"),
legend.text = element_text(size = 16,colour="black"),
strip.text.x = element_text(size = 16,colour="black"),
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_blank(),
legend.position = "none",
panel.grid.major = element_blank(), panel.grid.minor = element_blank()
) +
  facet_wrap(~pid_cat)

ggsave(file= paste("figures/si_s2_desc_dn.png", sep=""),width =25, height = 17, units = "cm", dpi=320)

###

## pol violence

dfs<- data %>% dplyr::select(all_of(c("viol_index", "pid_cat")))%>%
  mutate_all(~as.character(.)) %>%
pivot_longer(
cols = c( "viol_index"),
names_to = "category",
values_to = "value") %>%
  mutate(emotion_category = case_when(str_detect(category, "viol")~ "PV index",
                            str_detect(category, "demnorms") ~ "DN index")) %>%
  mutate(value=as.numeric(value))
  
ggplot(dfs, aes(x = value, y = emotion_category, fill=emotion_category)) +
  geom_density_ridges(
    jittered_points = TRUE,
    point_shape = "|", point_size = 2, size = 0.25,
    position = position_points_jitter(height = 0), alpha = 0.6, scale = 1.2,
    quantile_lines=TRUE, quantiles=2) +
  theme_classic()+
       labs(x = "Value", y="",title = "Distribution of support for political violence (Study 2)")+
  theme(
plot.title =element_text(size=16, colour="black", face = "bold"),
plot.subtitle = element_text(size=15, colour="black"),
axis.title.y = element_text(size=16, colour="black"),
axis.title.x = element_text(size=16,colour="black"),
axis.text.x = element_text(size=16,colour="black"),
axis.text.y =element_text(size=16,colour="black"),
legend.text = element_text(size = 16,colour="black"),
strip.text.x = element_text(size = 16,colour="black"),
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_blank(),
legend.position = "none",
panel.grid.major = element_blank(), panel.grid.minor = element_blank()
) +
  facet_wrap(~pid_cat)

ggsave(file= paste("figures/si_s2_desc_pv.png", sep=""),width =25, height = 17, units = "cm", dpi=320)


### ap
dv_cols <- data %>% select(starts_with(c("ap_", "therm")))   %>% colnames(.) 


dfs<- data %>% dplyr::select(all_of(c(dv_cols,"pid_cat")))%>%
  mutate_all(~as.character(.)) %>%
pivot_longer(
cols = all_of(c(dv_cols)),
names_to = "category",
values_to = "value") %>%
  mutate(Model=category,
    emotion_category = case_when(
      str_detect(Model, "ap_party") ~ "AP party",
      str_detect(Model, "ap_voters") ~ "AP voters",
      str_detect(Model, "ap_outgroup_party|therm_poloutparty") ~ "Therm (Out-party)",
      str_detect(Model, "ap_ingroup_party|therm_polinparty") ~ "Therm (In-party)",
      str_detect(Model, "ap_outgroup_voters|therm_votersoutparty") ~ "Therm (Out-voters)",
      str_detect(Model, "ap_ingroup_voters|therm_votersinparty") ~ "Therm (In-voters)",
      TRUE ~ "Other")) %>%
  mutate(value=as.numeric(value))
  
  
  

library(ggplot2)
ggplot(dfs, aes(x = value, y = emotion_category, fill=emotion_category)) +
  geom_density_ridges(
    jittered_points = TRUE,
    point_shape = "|", point_size = 2, size = 0.25,
    position = position_points_jitter(height = 0), alpha = 0.6, scale = 1.2,
    quantile_lines=TRUE, quantiles=2) +
  theme_classic()+
       labs(x = "Value", y="",title = "Distribution of affective polarization (Study 2)")+
  theme(
plot.title =element_text(size=16, colour="black", face = "bold"),
plot.subtitle = element_text(size=15, colour="black"),
axis.title.y = element_text(size=16, colour="black"),
axis.title.x = element_text(size=16,colour="black"),
axis.text.x = element_text(size=16,colour="black"),
axis.text.y =element_text(size=16,colour="black"),
legend.text = element_text(size = 16,colour="black"),
strip.text.x = element_text(size = 16,colour="black"),
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_blank(),
legend.position = "none",
panel.grid.major = element_blank(), panel.grid.minor = element_blank()
) +
  facet_wrap(~pid_cat)

ggsave(file= paste("figures/si_s2_desc_ap.png", sep=""),width =25, height = 17, units = "cm", dpi=320)

# stats for in-text description, Appendix

sd(data$w2_ap_party[data$pid_cat=="Democrats"])
summary(data$w2_ap_party[data$pid_cat=="Democrats"])

summary(data$therm_votersoutparty[data$pid_cat=="Democrats"])

sd(data$w2_ap_outgroup_party[data$pid_cat=="Democrats"])

summary(data$therm_poloutparty[data$pid_cat=="Democrats"])

sd(data$w2_ap_outgroup_voters[data$pid_cat=="Democrats"])
```
# Empathy mechanism


```{r empathy}



data <- data %>% mutate(manip.empath_outparty = as.numeric(manip.empath_1),
                        manip.empath_inpartyneg = as.numeric(manip.empath_16),
                        manip.empath_inpartypos = as.numeric(manip.empath_23),
                        empathtreat = manipword)


```

```{r empath}
## plot mean negative sentiment
library(broom)
library(estimatr)
summaryres1 <-
  data %>% 
  filter(treatment_factor=="control") %>%
  group_by(treatment_factor) %>%
  do(tidy(lm_robust(manip.empath_outparty ~ 1, data = .))) %>%
  mutate(predicted = estimate) %>%
  mutate(conf.low.83 = estimate -(std.error*1.3722),
                          conf.high.83 = estimate + (std.error*1.3722),
         conf.low = estimate -(std.error*1.96),
                          conf.high = estimate + (std.error*1.96)) %>%
  mutate(dv= "Enthusiastic out-partisan")

summaryres2 <-
  data %>% 
  filter(treatment_factor=="control") %>%
  group_by(treatment_factor) %>%
  filter(manipword=="angry") %>%
  do(tidy(lm_robust(manip.empath_inpartyneg ~ 1, data = .))) %>%
  mutate(predicted = estimate) %>%
  mutate(conf.low.83 = estimate -(std.error*1.3722),
                          conf.high.83 = estimate + (std.error*1.3722),
         conf.low = estimate -(std.error*1.96),
                          conf.high = estimate + (std.error*1.96)) %>%
  mutate(dv= "Angry in-partisan")

summaryres3 <-
  data %>% 
  filter(treatment_factor=="control") %>%
  group_by(treatment_factor) %>%
  filter(manipword=="worried") %>%
  do(tidy(lm_robust(manip.empath_inpartyneg ~ 1, data = .))) %>%
  mutate(predicted = estimate) %>%
  mutate(conf.low.83 = estimate -(std.error*1.3722),
                          conf.high.83 = estimate + (std.error*1.3722),
         conf.low = estimate -(std.error*1.96),
                          conf.high = estimate + (std.error*1.96)) %>%
  mutate(dv= "Worried in-partisan")

summaryres4 <-
  data %>% 
  filter(treatment_factor=="control") %>%
  group_by(treatment_factor) %>%
  do(tidy(lm_robust(manip.empath_inpartypos ~ 1, data = .))) %>%
  mutate(predicted = estimate) %>%
  mutate(conf.low.83 = estimate -(std.error*1.3722),
                          conf.high.83 = estimate + (std.error*1.3722),
         conf.low = estimate -(std.error*1.96),
                          conf.high = estimate + (std.error*1.96)) %>%
  mutate(dv= "Enthusiastic in-partisan")


summarydf <- summaryres1 %>% rbind(., summaryres2) %>%
  rbind(., summaryres3) %>%
  rbind(., summaryres4)

summarydf <- summarydf %>% mutate(dv = factor(dv, levels=c("Enthusiastic out-partisan", "Enthusiastic in-partisan", "Worried in-partisan", "Angry in-partisan")))
# just confidence intervals

library(ggplot2)
 summarydf %>%
ggplot(., aes(x=dv, y=predicted, color=dv, shape=dv)) +
  theme_minimal() +
  geom_point(size = 3, position = position_dodge2(width = 0.8)) +
  geom_linerange(
                aes(ymin = conf.low, ymax = conf.high), size=0.9, alpha=0.5, position = position_dodge2(width = 0.8)) +
  geom_linerange(aes(ymin=conf.low.83, ymax=conf.high.83), size=1.5, alpha=1.5, position = position_dodge2(width = 0.8)) +
  geom_point(position=position_jitterdodge(dodge.width=0.8), alpha=0.15) +
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=12),
axis.title.x = element_blank(),
axis.text.x = element_text(size=12),
axis.text.y =element_text(size=12),
legend.text = element_text(size = 12),
strip.text.x = element_text(size = 12),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=12)
) +
    labs(x = "", title = "Mean empathy scores" , caption = "83% CI (thick) and 95% CI (thin)") +
  coord_flip()+
  scale_color_manual(values=c('#636363','#810f7c','#43a2ca', '#636363','yellow3'))


# full graph, with underlying data

 summarydf %>% count(dv)

ggsave(file= "figures/s2_manip_empathy.png",width =25, height = 17, units = "cm", dpi=320)




summaryres_7 <-
  data %>% 
group_by(treatment_factor) %>%
  do(tidy(lm_robust(worried ~ 1, data = .))) %>%
  mutate(predicted = estimate) %>%
  mutate(conf.low.83 = estimate -(std.error*1.3722),
                          conf.high.83 = estimate + (std.error*1.3722),
         conf.low = estimate -(std.error*1.96),
                          conf.high = estimate + (std.error*1.96)) %>%
  mutate(dv= "Worried in-partisan")

summaryres_8 <-
  data %>% 
group_by(treatment_factor) %>%
  do(tidy(lm_robust(angry ~ 1, data = .))) %>%
  mutate(predicted = estimate) %>%
  mutate(conf.low.83 = estimate -(std.error*1.3722),
                          conf.high.83 = estimate + (std.error*1.3722),
         conf.low = estimate -(std.error*1.96),
                          conf.high = estimate + (std.error*1.96)) %>%
  mutate(dv= "Worried in-partisan")

summary(as.numeric(data$worried))
```




# Balance checks


```{r balance2, echo=F, results='asis'}
library(estimatr)
my_summarise <- function(data, group_var, summarise_var) {
  data %>%
    group_by(across({{ group_var }})) %>% 
    summarise(across({{ summarise_var }}, ~ paste(round(mean(., na.rm = T), digits = 2), " (", round(sd(., na.rm = T),digits = 2), ")", sep=""), .names = "{.col}")) #get mean and SD for each variable
}




subset <- data

balancetab <-  my_summarise(subset, treatment_factor,  c(w1_gender_female, w1_age, w1_education_advanced, w1_ethnicity_white,  w1_employ, pidstrength_numeric)) %>%
  pivot_longer(cols= !treatment_factor, names_to="variable",values_to="value") %>%
pivot_wider(names_from =  treatment_factor, values_from =value) %>%
  rename("Variable"="variable") %>%
  relocate(Variable) %>%
  rename(Placebo=control,
         Anger=anger,
         Worry=fear)







controlvars <- c("w1_gender_female", "w1_age", "w1_education_advanced", "w1_ethnicity_white",  "w1_employ", "pidstrength_numeric")

my_pvalue <- function(summarise_var) {
lm1 <- lm_robust((eval(parse(text=paste(summarise_var, sep="")))) ~ treatment_factor,  data=data)

model <- lm1 %>% tidy() %>% select(c(p.value, term)) %>% mutate(term=case_when(str_detect(tolower(term), "anger") ~ "Anger",str_detect(tolower(term), "fear") ~ "Worry",
                                                                      str_detect(tolower(term), "cept") ~ "Placebo")) %>%
  filter(term!="Placebo") %>%
  pivot_longer(cols= ! term, names_to="variable",values_to="value") %>%
pivot_wider(names_from =  term, values_from =value) %>%
  rename("Variable"="variable") %>%
  relocate(Variable)%>% mutate(Variable=paste0(summarise_var))
                                                                      
return(model)
}



m <- data.frame()
pvalue <- data.frame()
for (i in controlvars) {
  m <- my_pvalue(i)
pvalue <- rbind(pvalue, m)
}

balance <- balancetab %>% mutate(Anger= case_when(pvalue$Anger<0.05& pvalue$Anger>=0.01 ~ paste(Anger, "**"),
                                        pvalue$Anger<0.01 ~ paste(Anger, "***"),
                                        pvalue$Anger<0.1 & pvalue$Anger>=0.05  ~ paste(Anger, "*"),
                                        pvalue$Anger>=0.1   ~ paste(Anger))) %>%
 mutate(Worry= case_when(pvalue$Worry<0.05& pvalue$Worry>=0.01 ~ paste(Worry, "**"),
                                        pvalue$Worry<0.01 ~ paste(Worry, "***"),
                                        pvalue$Worry<0.1 & pvalue$Worry>=0.05  ~ paste(Worry, "*"),
                                        pvalue$Worry>=0.1   ~ paste(Worry)))


balance <- balance %>%
  mutate(Variable=case_when(str_detect(Variable, "gender")~"Gender=Female (0/1)",
                            str_detect(Variable, "age")~"Age",
                            str_detect(Variable, "education")~"Education=Advanced (0/1)",
                            str_detect(Variable, "white")~"Ethnicity=White (0/1)",
                            str_detect(Variable, "employ")~"In full-time employment (0/1)",
                            str_detect(Variable, "ap_voters")~"AP voters (-100 to +100)",
                            str_detect(Variable, "ap_party")~"AP parties (-100 to +100)",
                            str_detect(Variable, "strength")~"PID strength (1-5)",
                            str_detect(Variable, "viol_index")~"Support pol. violence (0-1)",
                            str_detect(Variable, "outgroup_negative")~"Neg. partisanship (1-5)",
                            str_detect(Variable, "ingroup_positive")~"Pos. partisanship (1-5)",
                            str_detect(Variable, "aversion")~"Outgroup aversion (1-5)",
                            str_detect(Variable, "anxiety")~"Outgroup anxiety (1-5)"))

library(stargazer)
stargazer(balance, summary = F, rownames = F, title= paste ("Balance on key pre-treatment covariates:", "Study 2",  sep=" " ),  notes = c("Mean and (SD) for key covariates." , "Stars show significant differences between the treatment and the placebo condition.", "***p$<$0.01, **p$<$0.05, *p$<$0.1"), out=paste0("figures/balancetable_", "study2", ".tex"))


```


# SI, Compare our sample to ANES

```{r anes}

subset <- subset %>% mutate(inparty = "Democrats")
balancetab <-  my_summarise(subset, inparty,  c(w1_gender_female, w1_age, w1_education_advanced, w1_ethnicity_white, ap_voters)) %>%
  pivot_longer(cols= !inparty, names_to="variable",values_to="value") %>%
pivot_wider(names_from =  inparty, values_from =value) %>%
  rename("Variable"="variable") %>%
  relocate(Variable) 



balance <- balancetab %>% 
  mutate(Variable=case_when(str_detect(Variable, "gender")~"Gender=Female (0/1)",
                            str_detect(Variable, "age")~"Age",
                            str_detect(Variable, "education")~"Education=Advanced (0/1)",
                            str_detect(Variable, "white")~"Ethnicity=White (0/1)",
                            str_detect(Variable, "income")~"Income=Above70k (0/1)",
                            str_detect(Variable, "ap_voters")~"AP voters (-100 to +100)",
                            str_detect(Variable, "ap_party")~"AP parties (-100 to +100)",
                            str_detect(Variable, "partyid")~"PID=Republican (0/1)",
                            str_detect(Variable, "viol_index")~"Support pol. violence (0-1)",
                            str_detect(Variable, "outgroup_negative")~"Neg. partisanship (1-5)",
                            str_detect(Variable, "ingroup_positive")~"Pos. partisanship (1-5)",
                            str_detect(Variable, "aversion")~"Outgroup aversion (1-5)",
                            str_detect(Variable, "anxiety")~"Outgroup anxiety (1-5)"))

stargazer(balance, summary = F, rownames = F, title= "Relevant Study 2 sample characteristics",  notes = c("Mean and (SD) for key covariates."), out=paste0("figures/balancetable_", "s2_aggregate", ".tex"))

```


# SI, Regression tables Study 2

```{r regtables-s2-funcs}


library(texreg)
library(estimatr)
library(rlang)


# Define control variables
controlvars <- c("w1_gender_female", "w1_age", "w1_education_advanced", 
                 "w1_ethnicity_white", "w1_employ", "pidstrength_numeric")


# Main function to run models 
run_models <- function(outcome, data) {
  control_formula <- paste(controlvars, collapse = " + ")

  models <- list(
    lm1 = lm_robust(as.formula(paste(outcome, "~ treatment_pooled")), data = data),
    lm2 = lm_robust(as.formula(paste(outcome, "~ treatment_factor")), data = data),
    lm3 = lm_robust(as.formula(paste(outcome, "~ treatment_pooled * pidstrength_numeric")), data = data),
    lm4 = lm_robust(as.formula(paste(outcome, "~ treatment_factor * pidstrength_numeric")), data = data),
    lm5 = lm_robust(as.formula(paste(outcome, "~ treatment_pooled +", control_formula)), data = data),
    lm6 = lm_robust(as.formula(paste(outcome, "~ treatment_factor +", control_formula)), data = data)
  )



  return(models)
}

# rename coefficients in texreg

coef_labels <- list(
  "(Intercept)"= "Intercept",
  "treatment_pooledPooled" = "Treatment=Pooled",
  "treatment_factoranger" = "Treatment=Anger",
  "treatment_factorfear" = "Treatment=Worry",
  "w1_gender_female" = "Gender=Female",
  "w1_age" = "Age",
  "w1_education_advanced" = "Education=Degree",
  "w1_ethnicity_white" = "Ethnicity=White",
  "w1_employ" = "Full-time employed",
  "pidstrength_numeric" = "Party importance",
  "treatment_pooledPooled:pidstrength_numeric" = "Treatment=Pooled:Party importance",
  "treatment_factoranger:pidstrength_numeric" = "Treatment=Anger:Party importance",
   "treatment_factorfear:pidstrength_numeric" = "Treatment=Worry:Party importance"
  
)



# get tex file 

regtables <- function(outcomevar, outcomenameshort, outcomenamelong){
 models <- run_models(outcomevar, data = data) 
 regfile <- texreg(models, include.ci = FALSE, 
                    digits = 3,
                    stars = c(0.01,0.05,0.1),
                    custom.model.names=c("M1", "M2", "M3", "M4", "M5", "M6"),
                  custom.coef.map = coef_labels,
                    caption= paste("Study 2: Effect of electoral loss treatments on \n DV: ", outcomenamelong,  sep=""),
                  custom.note = "\\item %stars. ",
                 threeparttable = T,
                    label= paste("table:s2_", outcomenameshort, "_reg", sep=""),
                 use.packages = FALSE, #this prevents the usepackage option from printing in the table in latex,
                 include.rmse = FALSE
                    )
print(regfile)
print(regfile, file =paste("figures/s2_", outcomenameshort, "_reg.tex", sep=""))
}

```

# Apply functions to generate regression tables

```{r applyreg}


regtables("viol_index", "viol_index", "support for political violence (index)")
regtables("demnorms_index", "demnorms_index", "support for democratic norms (index)")
regtables("pv4_normalized", "pv4_normalized", "support for election-specific violence")

regtables("worried_bin", "worried_bin", "feeling very or extremely worried about the election outcome (binary)")

regtables("angry_bin", "angry_bin", "feeling very or extremely angry about the election outcome (binary)")


```



# SI opentext graphs

```{r siopentext}
library(dplyr)
library(ggplot2)
library(broom)
library(estimatr)


data <- data %>% mutate(w1_partyid= "Democrats")
data <- data %>% mutate(treatment_factor_ren= case_when(treatment_factor=="anger" ~ "Anger",
                                                    treatment_factor=="fear" ~ "Worry",
                                                    treatment_factor=="control" ~ "Placebo"),
                        treatment_factor_ren = factor(treatment_factor_ren, levels=c("Placebo", "Anger", "Worry")))
plot_sentiment <- function(data, outcome_var, y_label, title_text, y_limits, file_name) {
  
  # Dynamically create formula
  formula <- as.formula(paste(outcome_var, "~ 1"))
  
  # Summary statistics
  summaryres <- data %>%
    group_by(treatment_factor_ren, w1_partyid) %>%
    do(tidy(lm_robust(formula, data = .))) %>%
    mutate(
      predicted = estimate,
      conf.low.83 = estimate - (std.error * 1.3722),
      conf.high.83 = estimate + (std.error * 1.3722),
      conf.low = estimate - (std.error * 1.96),
      conf.high = estimate + (std.error * 1.96)
    )
  
  
# Add predicted column to data
data$predicted <- data[[outcome_var]]

# Plot
p <- data %>%
  ggplot(aes(x = treatment_factor_ren, y = predicted,
               color = treatment_factor_ren,
               shape = treatment_factor_ren,
               fill = treatment_factor_ren)) +
    theme_minimal() +
    facet_wrap(~w1_partyid) +
    scale_color_manual(values = rep(c('#636363','#810f7c','#43a2ca'), 2)) +
    scale_fill_manual(values = rep(c('#636363','#810f7c','#43a2ca'), 2)) +
    ylim(y_limits[1], y_limits[2]) +
    geom_point(data = summaryres, size = 3, position = position_dodge2(width = 0.8)) +
    geom_linerange(data = summaryres,
                   aes(ymin = conf.low, ymax = conf.high),
                   size = 0.9, alpha = 0.5, position = position_dodge2(width = 0.8)) +
    geom_linerange(data = summaryres,
                   aes(ymin = conf.low.83, ymax = conf.high.83),
                   size = 1.5, alpha = 1.5, position = position_dodge2(width = 0.8)) +
    geom_point(position = position_jitterdodge(dodge.width = 0.8), alpha = 0.15) +
    theme(
      plot.title = element_text(hjust = 0, size = 12, face = "bold"),
      axis.title.y = element_text(size = 12),
      axis.title.x = element_blank(),
      axis.text.x = element_text(size = 12),
      axis.text.y = element_text(size = 12),
      legend.text = element_text(size = 12),
      strip.text.x = element_text(size = 12),
      legend.position = "none",
      legend.background = element_blank(),
      legend.box.background = element_rect(colour = "grey"),
      legend.title = element_text(size = 12)
    ) +
    labs(
      x = "Treatment condition",
      y = y_label,
      title = title_text,
      caption = "83% CI (thick) and 95% CI (thin)",
      color = "Electoral loss treatment",
      shape = "Electoral loss treatment",
      fill = "Electoral loss treatment"
    )
  
  ggsave(file = file_name, plot = p, width = 25, height = 17, units = "cm", dpi = 320)
}

# Example usage:
plot_sentiment(data, "sentiment_negative",
               "Number of negative words used in open text question (Bing dictionary)",
               "Mean negative sentiment across experimental conditions and partisanship",
               c(0, 4),
               "figures/si_s2_manip_opentext_sentiment_bing.png")

plot_sentiment(data, "sentiment_negative_afinn",
               "Mean sentiment in open text question (Afinn dictionary) \n More negative values = more negative sentiment",
               "Mean sentiment across experimental conditions and partisanship (Afinn dictionary)",
               c(-5, 5),
               "figures/si_s2_manip_opentext_sentiment_afinn.png")

plot_sentiment(data, "sentiment_negative_fear",
               "Number of fear-related words used in open text question (NRC dictionary)",
               "Mean fear-related words across experimental conditions and partisanship",
               c(0, 3),
               "figures/si_s2_manip_opentext_sentiment_nrc_fear.png")

plot_sentiment(data, "sentiment_negative_anger",
               "Number of angry words used in open text question (NRC dictionary)",
               "Mean anger-related words across experimental conditions and partisanship",
               c(0, 3),
               "figures/si_s2_manip_opentext_sentiment_nrc_anger.png")


````

# SI, Opentext N words

```{r nwords}

tidy_countwords <- tidy_countwords %>% mutate(number_words=n)
data <- left_join(data, tidy_countwords, by="X") 


data <- data %>% 
  mutate(nwords = replace_na(number_words, 0))



lmmodel <- data %>%
lm_robust(nwords~ treatment_factor_ren, data=.)


summaryres <-
  data %>% 
    group_by(treatment_factor_ren, w1_partyid) %>%
  do(tidy(lm_robust(nwords ~ 1, data = .))) %>%
  mutate(predicted = estimate) %>%
  mutate(conf.low.83 = estimate -(std.error*1.3722),
                          conf.high.83 = estimate + (std.error*1.3722),
         conf.low = estimate -(std.error*1.96),
                          conf.high = estimate + (std.error*1.96)) 




library(ggplot2)
#install.packages("ggdist")
library(ggdist)
data %>%
  mutate(predicted=nwords) %>%
ggplot(., aes(x=factor(treatment_factor_ren), y=predicted, color=factor(treatment_factor_ren), shape=factor(treatment_factor_ren), fill=factor(treatment_factor_ren))) +
  theme_minimal() +
stat_halfeye(
    # adjust bandwidth
    adjust = 1,
    # move to the right
    justification = -0.2,
    # remove the slub interval
    .width = 0,
    point_colour = NA,
    alpha=0.45,
    scale=0.6
  )+
  facet_wrap(~w1_partyid)+
  scale_color_manual(values=c('#636363','#810f7c','#43a2ca', '#636363','#810f7c','#43a2ca'))+
  scale_fill_manual(values=c('#636363','#810f7c','#43a2ca', '#636363','#810f7c','#43a2ca'))+
  ylim(0,100)+
  geom_point(data = summaryres, size = 3, position = position_dodge2(width = 0.8)) +
  geom_linerange(data = summaryres,
                aes(ymin = conf.low, ymax = conf.high), size=0.9, alpha=0.5, position = position_dodge2(width = 0.8)) +
  geom_linerange(data=summaryres, aes(ymin=conf.low.83, ymax=conf.high.83), size=1.5, alpha=1.5, position = position_dodge2(width = 0.8)) +
  geom_point(position=position_jitterdodge(dodge.width=0.8), alpha=0.15) +
  theme(
plot.title =element_text(size=16, colour="black", face = "bold"),
plot.subtitle = element_text(size=15, colour="black"),
axis.title.y = element_text(size=12, colour="black"),
axis.title.x = element_text(size=12,colour="black"),
axis.text.x = element_text(size=12,colour="black"),
axis.text.y =element_text(size=12,colour="black"),
legend.text = element_text(size = 12,colour="black"),
strip.text.x = element_text(size = 12,colour="black"),
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_blank(),
legend.position = "none",
panel.grid.major = element_blank(), panel.grid.minor = element_blank()
) +
    labs(x = "Treatment condition", y = "Number of words written in open text question" ,title= "Engagement with writing task (Study 2)", caption = "83% CI (thick) and 95% CI (thin)", color="Electoral loss treatment", group=NULL, shape="Electoral loss treatment", fill="Electoral loss treatment")


ggsave(file= "figures/si_s2_opentext_nwords.png",width =25, height = 17, units = "cm", dpi=320)


```


## SI opentext top words


```{r opentwords}




library(tidytext)
library(SnowballC)

tidy_opentext <- tidy_opentext %>% mutate(w1_partyid="Democrats")
    
top_words <-
   tidy_opentext %>%
      anti_join(stop_words) %>%
  mutate(stem = wordStem(word)) %>%
    group_by(treatment_factor, w1_partyid) %>%
            count(stem) %>%
              arrange(desc(n))


# create a joint colour scheme



words_dems <- top_words %>%
  filter(treatment_factor=="control") %>%
  filter(w1_partyid=="Democrats")%>%
  slice_head(., n = 20) %>% pull(stem)

ab <- c(words_dems)
ab_unique <- unique(ab)

palette1_named = setNames(object = scales::hue_pal()(length(ab_unique)), nm = ab_unique)
print(palette1_named)

# create plot 1 

g1_dems <- top_words %>%
  filter(treatment_factor=="control") %>%
  filter(w1_partyid=="Democrats") %>%
  slice_head(., n = 20) %>%
    ggplot(aes(x=reorder(stem, n), y=n, fill=stem))+
      geom_bar(stat="identity",  alpha=0.65)+
        theme_minimal()+
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=12),
axis.title.x = element_text(size=12),
axis.text.x = element_text(size=12),
axis.text.y =element_text(size=12),
legend.text = element_text(size = 12),
strip.text.x = element_text(size = 12),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=12)
) +
    labs(x = "", y = "Frequency" ,title= "Democrats")+
          guides(fill=FALSE)+
  coord_flip()+
  scale_fill_manual(values = palette1_named)
  
#
#install.packages("ggpubr")
library(ggpubr)
figure <- ggarrange(g1_dems, ncol = 1, nrow = 1)
annotate_figure(figure,top = text_grob("Most frequent word stems in the open text placebo question", color = "black", face = "bold", size = 14))

ggsave(file= "figures/si_s2_opentext_placebo_topwords.png",width =27, height = 14, units = "cm", dpi=320)

```

## SI openwords anger



```{r opentwords}




library(tidytext)
library(SnowballC)
# remove stop words


tidy_opentext <- tidy_opentext %>% mutate(w1_partyid="Democrats")
    
top_words <-
   tidy_opentext %>%
      anti_join(stop_words) %>%
  mutate(stem = wordStem(word)) %>%
    group_by(treatment_factor, w1_partyid) %>%
            count(stem) %>%
              arrange(desc(n))


# create a joint colour scheme



words_dems <- top_words %>%
  filter(treatment_factor=="anger") %>%
  filter(w1_partyid=="Democrats")%>%
  slice_head(., n = 20) %>% pull(stem)

ab <- c(words_dems)
ab_unique <- unique(ab)

palette1_named = setNames(object = scales::hue_pal()(length(ab_unique)), nm = ab_unique)
print(palette1_named)

# create plot 1 

g1_dems <- top_words %>%
  filter(treatment_factor=="anger") %>%
  filter(w1_partyid=="Democrats") %>%
  slice_head(., n = 20) %>%
    ggplot(aes(x=reorder(stem, n), y=n, fill=stem))+
      geom_bar(stat="identity",  alpha=0.65)+
        theme_minimal()+
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=12),
axis.title.x = element_text(size=12),
axis.text.x = element_text(size=12),
axis.text.y =element_text(size=12),
legend.text = element_text(size = 12),
strip.text.x = element_text(size = 12),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=12)
) +
    labs(x = "", y = "Frequency" ,title= "Democrats")+
          guides(fill=FALSE)+
  coord_flip()+
  scale_fill_manual(values = palette1_named)
  
#
#install.packages("ggpubr")
library(ggpubr)
figure <- ggarrange(g1_dems, ncol = 1, nrow = 1)
annotate_figure(figure,top = text_grob("Most frequent word stems in the open text (anger)", color = "black", face = "bold", size = 14))

ggsave(file= "figures/si_s2_opentext_anger_topwords.png",width =27, height = 14, units = "cm", dpi=320)

```

## SI opentext - worry




```{r opentwords}




library(tidytext)
library(SnowballC)
# remove stop words



tidy_opentext <- tidy_opentext %>% mutate(w1_partyid="Democrats")
    
top_words <-
   tidy_opentext %>%
      anti_join(stop_words) %>%
  mutate(stem = wordStem(word)) %>%
    group_by(treatment_factor, w1_partyid) %>%
            count(stem) %>%
              arrange(desc(n))


# create a joint colour scheme



words_dems <- top_words %>%
  filter(treatment_factor=="fear") %>%
  filter(w1_partyid=="Democrats")%>%
  slice_head(., n = 20) %>% pull(stem)

ab <- c(words_dems)
ab_unique <- unique(ab)

palette1_named = setNames(object = scales::hue_pal()(length(ab_unique)), nm = ab_unique)
print(palette1_named)

# create plot 1 

g1_dems <- top_words %>%
  filter(treatment_factor=="fear") %>%
  filter(w1_partyid=="Democrats") %>%
  slice_head(., n = 20) %>%
    ggplot(aes(x=reorder(stem, n), y=n, fill=stem))+
      geom_bar(stat="identity",  alpha=0.65)+
        theme_minimal()+
  theme(
plot.title = element_text(hjust = 0, size = 12, face="bold"),
axis.title.y = element_text(size=12),
axis.title.x = element_text(size=12),
axis.text.x = element_text(size=12),
axis.text.y =element_text(size=12),
legend.text = element_text(size = 12),
strip.text.x = element_text(size = 12),
legend.position = "none",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "grey"),
legend.title=element_text(size=12)
) +
    labs(x = "", y = "Frequency" ,title= "Democrats")+
          guides(fill=FALSE)+
  coord_flip()+
  scale_fill_manual(values = palette1_named)
  
#
#install.packages("ggpubr")
library(ggpubr)
figure <- ggarrange(g1_dems, ncol = 1, nrow = 1)
annotate_figure(figure,top = text_grob("Most frequent word stems in the open text (worry)", color = "black", face = "bold", size = 14))

ggsave(file= "figures/si_s2_opentext_worry_topwords.png",width =27, height = 14, units = "cm", dpi=320)

```